package com.hiof.moraa.server.dao;

import com.hiof.moraa.mapping.TblAthlete;
import com.hiof.moraa.mapping.TblAthleteHealthfeed;
import com.hiof.moraa.mapping.TblAthleteLiveLocation;
import com.hiof.moraa.server.service.model.HealthFeed;
import com.hiof.moraa.server.service.model.LiveLocationFeed;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Created by IntelliJ IDEA.
 * User: Bergy
 * Date: 7/6/12
 * Time: 7:53 AM
 * To change this template use File | Settings | File Templates.
 */
public class HibernateAthleteHealthFeedDAOImpl implements AthleteHealthFeedDAO {
    private SessionFactory factory;

    public SessionFactory getFactory() {
        return factory;
    }

    public void setFactory(SessionFactory factory) {
        this.factory = factory;
    }

    @Override
    public HealthFeed insertAthleteLiveLocationFeed(int athleteId, Timestamp timestamp, double bloodPressure, double heartBeatRate, String status) {
        TblAthleteHealthfeed athleteHealthFeed = new TblAthleteHealthfeed();
        try {
            TblAthlete athlete = (TblAthlete) factory.getCurrentSession().get(TblAthlete.class, athleteId);
            athleteHealthFeed.setBloodPressure(bloodPressure);
            athleteHealthFeed.setHeartbeatRate(heartBeatRate);
            athleteHealthFeed.setStatus(status);
            athleteHealthFeed.setTblAthlete(athlete);
            athleteHealthFeed.setTimeStamp(timestamp);

            factory.getCurrentSession().save(athleteHealthFeed);
        } catch (HibernateException e) {
            Logger.getLogger(HibernateAthleteHealthFeedDAOImpl.class.getName()).log(Level.SEVERE, "Hibernate Exception Occured", e);
        }

        HealthFeed healthFeed = convert2HealthFeed(athleteHealthFeed);
        return healthFeed;
    }

    @Override
    public List<HealthFeed> getHealthFeedHistoryOfAthlete(int athleteId) {
        try {
            Session session = factory.getCurrentSession();

            String hql = "from TblAthleteHealthfeed healthFeed where healthFeed.tblAthlete.id = :athleteId "
                    + "order by healthFeed.timeStamp";
            Query query = session.createQuery(hql);
            query.setInteger("athleteId", athleteId);
            List<TblAthleteHealthfeed> athleteHealthFeedList = query.list();

            List<HealthFeed> result = fetchToHealthFeednList(athleteHealthFeedList);
            athleteHealthFeedList.clear();
            return result;
        } catch (Exception ex) {
            return new ArrayList<HealthFeed>();
        }
    }

    private List<HealthFeed> fetchToHealthFeednList(List<TblAthleteHealthfeed> athleteHealthFeedList) {
        List<HealthFeed> result = new ArrayList<HealthFeed>();
        for (TblAthleteHealthfeed healthFeed : athleteHealthFeedList) {
            result.add(convert2HealthFeed(healthFeed));
        }
        return result;
    }

    private HealthFeed convert2HealthFeed(TblAthleteHealthfeed athleteHealthFeed) {
        if(athleteHealthFeed == null)
            return null;
        HealthFeed healthFeed = new HealthFeed(
                athleteHealthFeed.getTblAthlete().getTblUser().getId(),
                athleteHealthFeed.getTblAthlete().getTblUser().getUsername(),
                athleteHealthFeed.getTblAthlete().getTblUser().getFirstName() + " " + athleteHealthFeed.getTblAthlete().getTblUser().getLastName(),
                athleteHealthFeed.getTblAthlete().getTblUser().getProfilePicture(),
                athleteHealthFeed.getTimeStamp(),
                athleteHealthFeed.getTblAthlete().getAttendantCode(),
                athleteHealthFeed.getHeartbeatRate(),
                athleteHealthFeed.getStatus());
        return healthFeed;
    }
}
